package com.zzw.design.observer;

import java.util.HashSet;
import java.util.Set;

public class Writer1 {
	
	private String name;//���ߵ�����
    
    private String lastNovel;//��¼�������·�����С˵
    
    private Set<WriterListener> writerListenerList = new HashSet<WriterListener>();//������Ҫ����һ���Լ����������б�

    public Writer1(String name) {
        super();
        this.name = name;
        WriterManager.getInstance().add(this);
    }

    //���߷�����С˵�ˣ�Ҫ֪ͨ���й�ע�Լ��Ķ���
    public void addNovel(String novel) {
        System.out.println(name + "���������顶" + novel + "����");
        lastNovel = novel;
        fireEvent();
    }
    
    //��������������¼���֪ͨ���м�������µļ�����
    private void fireEvent(){
        WriterEvent writerEvent = new WriterEvent(this);
        for (WriterListener writerListener : writerListenerList) {
            writerListener.addNovel(writerEvent);
        }
    }
    
    //�ṩ���ⲿע���Ϊ�Լ��ļ������ķ���
    public void registerListener(WriterListener writerListener){
        writerListenerList.add(writerListener);
    }
    
    //�ṩ���ⲿע���ķ���
    public void unregisterListener(WriterListener writerListener){
        writerListenerList.remove(writerListener);
    }
    
    public String getLastNovel() {
        return lastNovel;
    }

    public String getName() {
        return name;
    }
}